home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bchelp10.zip / TI642.ASC < prev    next >
Text File  |  1991-09-11  |  3KB  |  133 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                            NUMBER  :  642
  9.   VERSION  :  2.0
  10.        OS  :  PC DOS
  11.      DATE  :  September 11, 1991                       PAGE  :  1/2
  12.  
  13.     TITLE  :  "Duplicate Symbol" Error on 'extern'ed Variable(s)
  14.  
  15.  
  16.  
  17.  
  18.   All variables declared globally without the static  storage class
  19.   modifier become public symbols.   So  the  linker  will encounter
  20.   them when processing that  obj  file.  Another module will not be
  21.   able to refer to  them,  because  the  compiler will not generate
  22.   code  that  references  them unless it has type information.  One
  23.   gives  this type information to the  compiler  by  declaring  the
  24.   variables as externs within that module.   Then the  compiler can
  25.   generate proper code and let the linker fix up references  to the
  26.   variable.
  27.  
  28.   NOTE, however, that whenever you initialize  a  variable, whether
  29.   extern or not, the compiler  must create storage for the variable
  30.   within that module, even  with  externs.  Why?  The compiler does
  31.   not have access  to  the  module  in which the extern variable is
  32.   actually stored.  If it were to modify the value of  that storage
  33.   location, it would mean passing a message on to the linker  to do
  34.   so, and there really  is  no mechanism for doing that through the
  35.   intel/hex  format  for  object  modules  unless  you   wanted  to
  36.   introduce some nonstandard record within the obj files.    So the
  37.   compiler creates storage for the variable within that module.
  38.  
  39.   For example,
  40.  
  41.                            extern int a = 0;
  42.  
  43.   has the same affect as
  44.  
  45.                               int a = 0;
  46.  
  47.   A common situation in which this occurs involves global variables
  48.   such as _stklen, which specifies the  size  of the stack.  If one
  49.   uses _stklen to  set the size of the stack like this:
  50.  
  51.                     extern unsigned _stklen = 8192;
  52.  
  53.   a "duplicate symbol" warning will be returned  from  the compiler
  54.   for the reason discussed above.
  55.  
  56.   Breaking a  program into several modules, possibly to overcome an
  57.   "Out  of  memory"  compiler error, is another  common  source  of
  58.   "duplicate symbol" warnings.  This  occurs because one or more of
  59.   the existing symbols were  both  declared  and initialized at the
  60.   same time:
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                            NUMBER  :  642
  75.   VERSION  :  2.0
  76.        OS  :  PC DOS
  77.      DATE  :  September 11, 1991                       PAGE  :  2/2
  78.  
  79.     TITLE  :  "Duplicate Symbol" Error on 'extern'ed Variable(s)
  80.  
  81.  
  82.  
  83.  
  84.                            int avariable = 1;
  85.  
  86.   After  breaking  up  the  program,  this  declaration   is  moved
  87.   wholesale into a header file and prefixed with the extern keyword
  88.   in an attempt to provide that this variable can  be  seen  across
  89.   modules.    Subsequent  attempts  to  compile  the  project will,
  90.   however, result in  "duplicate  symbol" warnings.  To correct the
  91.   problem, remove the initialization so that the line in the header
  92.   file appears as:
  93.  
  94.                            int avariable;
  95.  
  96.   and initialize avariable in the source file where it is declared.
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.